package com.da.lon.wang.xlg.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.da.lon.wang.xlg.R;
import com.da.lon.wang.xlg.bean.SkuListResult.RecommendInfo;
import com.da.lon.wang.xlg.bean.SkuListResult.SkuInfo;
import com.da.lon.wang.xlg.view.SureSkuPopupWindow;

import java.util.List;

public class SkuAdapter extends BaseAdapter {

    ViewHolder holder = null;
    private LayoutInflater inflater;
    private List<RecommendInfo> list;
    private Context context;
    private int wai_index;
    private SkuInfo datas;

    public SkuAdapter(Context context, List<RecommendInfo> cateList,
                      SkuInfo datas, int wai_index) {
        inflater = LayoutInflater.from(context);
        this.wai_index = wai_index;
        this.datas = datas;
        this.list = cateList;
        this.context = context;
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        if (convertView == null) {
            holder = new ViewHolder();
            convertView = inflater
                    .inflate(R.layout.ocoffee_sku_grid_item, null);
            holder.all_max_rl = (RelativeLayout) convertView
                    .findViewById(R.id.all_max_rl);
            holder.sku_di_iv = (ImageView) convertView
                    .findViewById(R.id.sku_diy_iv);
            holder.sku_name_tv = (TextView) convertView
                    .findViewById(R.id.sku_name_tv);
            holder.sku_dis_tv = (TextView) convertView
                    .findViewById(R.id.sku_dis_tv);
            holder.sku_name_tv.setText(list.get(position).getStore_name());
            holder.sku_dis_tv.setText(list.get(position).getDescription());
            String state = list.get(position).getState();
            if (!state.isEmpty() && "1".equals(state)) {
                holder.sku_di_iv.setImageResource(R.drawable.sku_isopending);
            } else {
                holder.sku_di_iv.setImageResource(R.drawable.sku_isnoopend);
                holder.sku_name_tv.setTextColor(R.color.greay_line);

            }
            holder.sku_name_tv.setTag(wai_index);

            holder.all_max_rl.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    int tag = (int) holder.sku_name_tv.getTag();
                    String state = datas.getArea_list().get(tag - 1)
                            .getStore_list().get(position).getState();
                    if (!state.isEmpty() && "1".equals(state)) {
                        SureSkuPopupWindow sureSkuPopupWindow = new SureSkuPopupWindow(
                                context, datas, 1, tag, position);
                        // 显示窗口
                        sureSkuPopupWindow.showAtLocation(holder.all_max_rl,
                                Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL, 0,
                                0);
                    }
                }
            });
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }

        return convertView;
    }

    class ViewHolder {
        public RelativeLayout all_max_rl;
        public TextView sku_dis_tv;
        ImageView sku_di_iv;
        TextView sku_name_tv;
    }

}
